package com.study.springcloud.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.study.springcloud.service.PaymentHystrixService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author xujm
 * @version V2.4
 * @since 2.4.0 2021/4/11 17:03
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_global_fallback_method") //1.使用@DefaultProperties 定义全局服务降级方法
public class OrderHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    /**
     * 这种情况，使用全局服务降级方法
     */
    @GetMapping("/consumer/payment/hystrix/get/{id}")
    @HystrixCommand //3.使用 @HystrixCommand 使用全局服务降级方法
    public String paymentInfo_xxx(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        return result;
    }

    /**
     * 这种情况，使用指定的服务降级方法
     */
    @GetMapping("/consumer/payment/hystrix/get2/{id}")
    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")  //1.5秒钟以内就是正常的业务逻辑
    })
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        return result;
    }

    //指定服务降级方法
    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
        return "我是消费者80，对付支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,(┬＿┬)";
    }

    //2.定义全局服务降级方法(不能有参数)
    public String payment_global_fallback_method(){
        return "~~~~我是,全局服务降级方法";
    }
}

